import { setCatalog } from "./auth";
import { listMenuBtn } from "../api/token";
import querystring from "querystring";
import { SYSTEMTYPE } from "../config";
let Wi = window;

/**
 * 生成随机16未序列号
 */
export function guid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    const r = (Math.random() * 16) | 0;
    const v = c === "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

/**
 * 清除cookie
 */
export function clearCookie() {
  const keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if (keys) {
    for (let i = keys.length; i--; ) {
      document.cookie = keys[i] + "=0;expires=" + new Date(0).toUTCString();
    }
  }
}

export function mapTree(
  tree,
  iterator,
  level = 1,
  depthFirst = false,
  paths = []
) {
  return tree.map((item, index) => {
    if (depthFirst) {
      let children = item.children
        ? mapTree(
            item.children,
            iterator,
            level + 1,
            depthFirst,
            paths.concat(item)
          )
        : undefined;
      children && (item = { ...item, children: children });
      item = iterator(item, index, level, paths) || { ...item };
      return item;
    }

    item = iterator(item, index, level, paths) || { ...item };

    if (item.children && item.children.splice) {
      item.children = mapTree(
        item.children,
        iterator,
        level + 1,
        depthFirst,
        paths.concat(item)
      );
    }

    return item;
  });
}
